<?php
#
# author: Phil Spagnolo
# (c)2008 South Hill Calvary Chapel
#

include_once("./root.inc.php");
include_once("${root}/common.php");

session_start();

function prayer_div($bool) {
  if ($bool) {
    return "prayer_fail";
  }
  return "prayer_pass";
}

function check_exist($value) {
  if (!ereg("[A-Za-z]",$value)) {
    return FALSE;
  } else {
    return TRUE;
  }
}

function form_input($input, $error) {
  printf("<form method='post' action='prayer_request.php'>\n");
  printf("\t<div class='%s'>\n", prayer_div($error[0]));
  printf("\t\tFirst Name: <input name='first' type='text' value='%s'/><br />\n",$input[0]);
  printf("\t</div>\n");
  printf("\t<div class='%s'>\n", prayer_div($error[1]));
  printf("\t\tLast Name: <input name='last' type='text' value='%s'/><br />\n",$input[1]);
  printf("\t</div>\n");
  printf("\t<div class='%s'>\n", prayer_div($error[2]));
  printf("\t\tEmail: <input name='email' type='text' value='%s'/><br />\n",$input[2]);
  printf("\t</div>\n");
  printf("\t<div class='%s'>\n", prayer_div($error[3]));
  printf("\t\tDo you attend SHCC? \n");
  if ($input[3] == "T") {
    printf("\t\t<input type='radio' name='attend' value='T' checked='checked'>Yes\n");
    printf("\t\t<input type='radio' name='attend' value='F'> No<br />\n");
  } else if ($input[3] == "F") {
    printf("\t\t<input type='radio' name='attend' value='T'>Yes\n");
    printf("\t\t<input type='radio' name='attend' value='F' checked='checked'> No<br />\n");
  } else {
    printf("\t\t<input type='radio' name='attend' value='T'>Yes\n");
    printf("\t\t<input type='radio' name='attend' value='F'>No<br />\n");
  }
  printf("\t</div>\n");
  printf("\t<div class='%s'>\n", prayer_div($error[4]));
  printf("\t\tWhat is the nature of your prayer?\n");
  if ($input[4] == "Prayer Request") {
    printf("\t\t<input type='radio' name='prayer_type' value='Prayer Request' checked='checked'> Prayer Request\n");
    printf("\t\t<input type='radio' name='prayer_type' value='Praise Report'> Praise Report<br />\n");
  } else if ($input[4] == "Praise Report") {
    printf("\t\t<input type='radio' name='prayer_type' value='Prayer Request'> Prayer Request\n");
    printf("\t\t<input type='radio' name='prayer_type' value='Praise Report' checked='checked'> Praise Report<br />\n");
  } else {
    printf("\t\t<input type='radio' name='prayer_type' value='Prayer Request'> Prayer Request\n");
    printf("\t\t<input type='radio' name='prayer_type' value='Praise Report'> Praise Report<br />\n");
  }
  printf("\t</div>\n");
  printf("\t<div class='%s'>\n", prayer_div($error[5]));
  printf("\t\tSubject: <input name='subject' type='text' value='%s'/><br />\n",$input[5]);
  printf("\t</div>\n");
  printf("\t<div class='%s'>\n", prayer_div($error[6]));
  printf("\t\tEnter your prayer request or praise report here:<br />\n");
  printf("\t\t<textarea name='prayer' rows='10' cols='80'>%s</textarea><br />\n",$input[6]);
  printf("\t</div>\n");

  printf("\t<div class='%s'>\n", prayer_div($error[7]));
  printf("\t\t<b>Image Verification</b><br>\n");
  printf("\t\tTo prevent SPAM, please enter the string shown in the image.<br> The possible characters are letters from A to Z in capitalized form and the numbers from 0 to 9.  Thank You!\n");
  printf("\t</div>\n");

  printf("\t<table class='std'>\n");
  printf("\t\t<tr>\n");
  printf("\t\t\t<td colspan='2'> \n");
  printf("\t\t\t\t<input name='number' type='text' id=\"number\">\n");
  printf("\t\t\t</td>\n");
  printf("\t\t</tr>\n");
  printf("\t\t<tr>\n");
  printf("\t\t\t<td>\n");
  printf("\t\t\t\t<img src='random_image.php'>\n");
  printf("\t\t\t</td>\n");
  printf("\t\t\t<td><input name='Submit' type='submit' value='Submit Prayer'></td>\n");
  printf("\t\t</tr>\n");
  printf("\t</table>\n");

  printf("</form>\n");

}

$title = "Prayer Requests";
$meta_key= 'prayer, thanksgiving, praise, pray, request, petition, report';
$meta_desc="A page to submit prayer requests or praise reports that will be prayed for by our prayer team.";
print_html_head($title ,$meta_key,$meta_desc);

print_java_head(NULL);
begin_body();
page_title($title);

if (isset($_POST['Submit']))
{
  $first = $input[0] = $_REQUEST['first'];
  $last = $input[1] = $_REQUEST['last'];
  $email = $input[2] = $_REQUEST['email']; 
  $attend = $input[3] = $_REQUEST['attend'];
  $prayer_type = $input[4] = $_REQUEST['prayer_type'];
  $subject = $input[5] = $_REQUEST['subject'];
  $prayer = $input[6] = $_REQUEST['prayer'];
  $number = $input[7] = $_POST['number'];

  //check if the email address is invalid

  $failcheck=FALSE;
  for ($i=0; $i <= 7; $i++) {
    if ($i == 2) {
      $error[$i] = !check_email_address($email);
    } else if ($i == 7) {
      if(md5($number) != $_SESSION['image_value']) {
        $error[$i] = 1;
      }
    } else {
      $error[$i] = !check_exist($input[$i]); 
    }
    if ($error[$i]) {
      $failcheck=TRUE;
    }
  }

  if ($failcheck)
  {
    printf("<div class='paragraph'>\n");
    printf("\t<font color='red'>Please enter valid entries. Errors have occured where the text is red.</font>\n");
    printf("</div>\n");
    form_input($input, $error);
  }
  else
  {//send email
    if ($attend == 'T') {
      $message = $prayer_type." from ".$first." ".$last." who attends SHCC:  ".$prayer;
    } else {
      $message = $prayer_type." from ".$first." ".$last." who does not attend SHCC:  ".$prayer;
    }

  mail("prayer@southhillcalvary.org", $subject, htmlentities($message), "From: $email" );

  printf("<div class='paragraph'>\n");
  printf("\t%s %s,\n", $first, $last);
  printf("</div>\n");
  printf("<div class='paragraph'>\n");
  printf("\tThank you for submitting your %s.  It is our honor to pray for you.  Please let us know how you are doing by reporting back.\n", $prayer_type);
  printf("</div>\n");
  printf("<div class='paragraph'>\n");
  printf("Subject:  %s\n", $subject);
  printf("</div>\n");
  printf("<div class='paragraph'>\n");
  printf("%s:  %s\n", $prayer_type, $prayer);
  printf("</div>\n");
  }
}
else
{
  $input = array("","","","","","","","");
  $error = array(0,0,0,0,0,0,0,0);
  form_input($input, $error);
}

end_page_title();
end_body();
?>
